package serverSocket;

import java.net.*;
import java.util.Scanner;
import java.io.*;

import DBComm.DBComm;

public class Server
{
	public static void main(String[] args)
	{
	/*
		ServerProcess s1 = new ServerProcess();
		s1.start();
		Scanner s = new Scanner(System.in);
		while(s.hasNextLine()){
			if(s.equals("stats")){
				DBComm.outStats();
			}
		}
	}*/
		
		ServerSocket serverSocket = null;
		boolean listening = true;
		int port = 4444;

		try {
			serverSocket = new ServerSocket(port);
			
			// print out the server address
			InetAddress i = InetAddress.getLocalHost();
			
			System.out.println("Server host name: " + i.getHostName());
			System.out.println("Server host IP: " + i.getHostAddress());
			System.out.println("Listening for connections on port " + port);
		} catch (IOException e) {
			System.err.println("Could not listen on port " + port);
			System.exit(-1);
		}

		try
		{
			while (listening) // listen for connections
			{
				// accept a new connection
				Socket s = serverSocket.accept();
				s.setKeepAlive(true);
				ServerThread clientConnection = new ServerThread(s);
				
				// add the new thread to the HashMap
				ServerThreadStaticHash.addServerThread("Client", clientConnection);
				
				clientConnection.start();
				
				//new ServerThread(serverSocket.accept()).start();
				System.out.println("Connection accepted");
			}
	
			System.out.println("Server socket closed");
			serverSocket.close();
		}
		catch(Exception e)
		{
			System.err.println("Server exception: " + e.getClass().getSimpleName() + " - " + e.getMessage());
			e.printStackTrace();
		}
	}
}
